package com.woniuxy.profile.service.impl;

import com.woniuxy.commons.dto.ProfileDto;
import com.woniuxy.commons.entity.PageBean;
import com.woniuxy.commons.entity.Profile;
import com.woniuxy.profile.mapper.ProfileMapper;
import com.woniuxy.profile.service.ProfileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: dengzj
 * @Description:
 * @Date: Created in 14:41 2020/7/28
 */
@Service
public class ProfileServiceImpl implements ProfileService {

    @Autowired
    private ProfileMapper profileMapper;

    @Override
    public int insertProfile(Profile profile) {
        profileMapper.insertProfile(profile);
        return 1;
    }

    @Override
    public Profile findProfileByUid(int uid) {
        Profile findProfile = profileMapper.findProfileByUid(uid);
        return findProfile;
    }

    @Override
    public int updateProfile(Profile profile) {
        profileMapper.updateProfile(profile);
        return 1;
    }

    @Override
    public int deleteProfile(int uid) {
        profileMapper.deleteProfile(uid);
        return 1;
    }

    @Override
    public PageBean<Profile> findProfileByCondition(ProfileDto profileDto, int page) {
        PageBean<Profile> profilePageBean = new PageBean<>();
        int index = (page - 1) * 8;
        profileDto.setIndex(index).setPageSize(8);
        List<Profile> data = profileMapper.findProfileByCondition(profileDto);
        int totalNum = profileMapper.countOfProfile();
        profilePageBean.setData(data);
        profilePageBean.setTotalNum(totalNum);
        profilePageBean.setTotalPage(totalNum % 8 == 0 ? totalNum / 8 : totalNum / 8 + 1);
        profilePageBean.setNumsOfCurrPage(data.size());
        profilePageBean.setPage(page);
        profilePageBean.setPageSize(8);
        return profilePageBean;
    }

    @Override
    public int deleteProfileBatch(int[] batch) {
        profileMapper.deleteProfileBatch(batch);
        return 1;
    }
}
